package com.notemanagement.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.notemanagement.R;
import com.notemanagement.bean.OrderDetailInfo;
import com.notemanagement.utils.Constant;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * Created by 金 on 2015/1/17.
 */
public class AddOrder extends Activity implements View.OnClickListener {
    private Spinner spinner1, spinner2, spinner3, spinner4, spinner5;
    private ArrayList<String> list;
    private EditText name, phone, address, num1, num2, num3, num4, num5;
    private TextView addText;
    private ImageView back_image;
    private DatePicker datePicker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_order);

        initSpinner();
        initView();
    }

    private void initView() {
        name = (EditText) findViewById(R.id.new_order_name);
        phone = (EditText) findViewById(R.id.new_order_phone);
        address = (EditText) findViewById(R.id.new_order_address);
        num1 = (EditText) findViewById(R.id.new_order_product1_num);
        num2 = (EditText) findViewById(R.id.new_order_product2_num);
        num3 = (EditText) findViewById(R.id.new_order_product3_num);
        num4 = (EditText) findViewById(R.id.new_order_product4_num);
        num5 = (EditText) findViewById(R.id.new_order_product5_num);
        addText = (TextView) findViewById(R.id.order_add_order);
        back_image = (ImageView) findViewById(R.id.new_order_head_back);

        addText.setOnClickListener(this);
        back_image.setOnClickListener(this);
    }

    private void initSpinner() {
        list = new ArrayList<String>();
        spinner1 = (Spinner) findViewById(R.id.new_order_product1);
        spinner2 = (Spinner) findViewById(R.id.new_order_product2);
        spinner3 = (Spinner) findViewById(R.id.new_order_product3);
        spinner4 = (Spinner) findViewById(R.id.new_order_product4);
        spinner5 = (Spinner) findViewById(R.id.new_order_product5);

        Calendar calendar=Calendar.getInstance();
        int year=calendar.get(Calendar.YEAR);
        int monthOfYear=calendar.get(Calendar.MONTH);
        int dayOfMonth=calendar.get(Calendar.DAY_OF_MONTH);
        datePicker = (DatePicker) findViewById(R.id.new_order_date);
        datePicker.init(year, monthOfYear, dayOfMonth, null);

        list = Constant.db.queryProductItems();
        ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        spinner1.setAdapter(spinnerAdapter);
        spinner2.setAdapter(spinnerAdapter);
        spinner3.setAdapter(spinnerAdapter);
        spinner4.setAdapter(spinnerAdapter);
        spinner5.setAdapter(spinnerAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.order_add_order:
                verifyData();
                break;
            case R.id.new_order_head_back:
                setResult(1);
                this.finish();
                break;
        }
    }

    private void verifyData() {
        if (name.getText().toString().trim().equals("")) {
            name.setError("姓名不能为空!");
        } else if (phone.getText().toString().trim().equals(""))
            phone.setError("电话不能为空!");
        else if (address.getText().toString().trim().equals(""))
            address.setError("地址不能为空!");
        else if (spinner1.getSelectedItem() == 0)
            Toast.makeText(this, "至少添加一种商品!", Toast.LENGTH_SHORT).show();
        else if (num1.getText().toString().equals(""))
            num1.setError("数量不能为空!");
        else if (!spinner2.getSelectedItem().equals("") && num2.getText().toString().trim().equals(""))
            num2.setError("数量不能为空!");
        else if (!spinner3.getSelectedItem().equals("") && num3.getText().toString().trim().equals(""))
            num3.setError("数量不能为空!");
        else if (!spinner4.getSelectedItem().equals("") && num4.getText().toString().trim().equals(""))
            num4.setError("数量不能为空!");
        else if (!spinner5.getSelectedItem().equals("") && num5.getText().toString().trim().equals(""))
            num5.setError("数量不能为空!");
        else {
            OrderDetailInfo info = new OrderDetailInfo();
            info.name = name.getText().toString().trim();
            info.phone = phone.getText().toString().trim();
            info.address = address.getText().toString().trim();
            info.date = String.format("%04d", datePicker.getYear()) + String.format("%02d", datePicker.getMonth() + 1) + String.format("%02d", datePicker.getDayOfMonth());
            info.materials.add((String)spinner1.getSelectedItem());
            info.materialNum.add(Integer.parseInt(num1.getText().toString().trim()));
            if(!spinner2.getSelectedItem().equals("")) {
                info.materials.add((String)spinner2.getSelectedItem());
                info.materialNum.add(Integer.parseInt(num2.getText().toString().trim()));
            }
            else{
                info.materials.add((String)spinner2.getSelectedItem());
                info.materialNum.add(0);
            }

            if(!spinner3.getSelectedItem().equals("")) {
                info.materials.add((String)spinner3.getSelectedItem());
                info.materialNum.add(Integer.parseInt(num3.getText().toString().trim()));
            }
            else{
                info.materials.add((String)spinner3.getSelectedItem());
                info.materialNum.add(0);
            }

            if(!spinner4.getSelectedItem().equals("")) {
                info.materials.add((String)spinner4.getSelectedItem());
                info.materialNum.add(Integer.parseInt(num4.getText().toString().trim()));
            }
            else{
                info.materials.add((String)spinner4.getSelectedItem());
                info.materialNum.add(0);
            }

            if(!spinner5.getSelectedItem().equals("")) {
                info.materials.add((String)spinner5.getSelectedItem());
                info.materialNum.add(Integer.parseInt(num5.getText().toString().trim()));
            }
            else{
                info.materials.add((String)spinner5.getSelectedItem());
                info.materialNum.add(0);
            }

            long id = Constant.db.insertOrderDetail(info);
            if(id < 0){
                Toast.makeText(this, "订单生成失败，请重新下单!", Toast.LENGTH_LONG).show();
                this.finish();
            }
            info.id = id;
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("detail", info);
            intent.putExtra("bundle", bundle);
            setResult(0, intent);
            this.finish();
        }

    }
}
